package org.xa3apm.vote.hibernate.bean;

import java.io.Serializable;
import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.xa3apm.vote.hibernate.entities.Municipality;
import org.xa3apm.vote.hibernate.entities.Province;

@Stateless
public class MunicipalityBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3760560614961332869L;

	@PersistenceContext(unitName = "vote")
	private EntityManager em;

	public Municipality getById(Long id) {
		return (Municipality) (em.createNamedQuery("Municipality.getById")
				.setParameter("id", id).getSingleResult());
	}

	public Collection<Municipality> getAll() {
		@SuppressWarnings("unchecked")
		Collection<Municipality> resultList = em.createNamedQuery(
				"Municipality.getAll").getResultList();
		return resultList;
	}

	public Collection<Municipality> getAllByProvinceId(Province province) {
		@SuppressWarnings("unchecked")
		Collection<Municipality> resultList = em
				.createNamedQuery("Municipality.getAllByProvinceId")
				.setParameter("province", province).getResultList();
		return resultList;
	}

}
